package com.suyang.syt.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suyang.syt.dao.interfaces.PermissionDao;
import com.suyang.syt.model.Permission;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.PagerEasyUI;
import com.suyang.syt.model.dto.PermissionDTO;
import com.suyang.syt.service.interfaces.PermissionService;

/**
 * @author administrator
 * @date 2016-2-24 10:56:42
 * @description
 */
@Service
public class PermissionServiceImpl implements PermissionService {
	@Autowired
	private PermissionDao permissionDao;

	@Override
	public PagerEasyUI<PermissionDTO> listByEasyUI(int page, int rows, String sort, String order, String createDate1, String createDate2) throws Exception {
		StringBuffer hql = new StringBuffer(" from Permission Pn where 1=1 ");
		if (StringUtils.isNotEmpty(createDate1)) {
			hql.append(" and Pn.createTime >= '").append(createDate1).append("' ");
		}
		if (StringUtils.isNotEmpty(createDate2)) {
			hql.append(" and Pn.createTime <= '").append(createDate2).append("' ");
		}
		if (StringUtils.isNotEmpty(sort)) {
			hql.append(" order by Pn.").append(sort);
		}
		if (StringUtils.isNotEmpty(order)) {
			hql.append(" ").append(order);
		}
		PagerEasyUI<PermissionDTO> easyUIPager = new PagerEasyUI<PermissionDTO>();
		List<Permission> permissions = permissionDao.findByEasyUI(hql.toString(), page, rows);
		List<PermissionDTO> permissionDTOs = new ArrayList<PermissionDTO>();
		PermissionDTO permissionDTO = null;
		for (Permission permission : permissions) {
			permissionDTO = new PermissionDTO(permission);
			permissionDTOs.add(permissionDTO);
		}
		easyUIPager.setRows(permissionDTOs);
		easyUIPager.setTotal((Long) permissionDao.queryByHql(" select count(*) " + hql.substring(0, hql.indexOf("order by"))));
		return easyUIPager;
	}

	private Permission setPermission(Permission permission, PermissionDTO permissionDTO, boolean isAdd) {
		permission.setStatus(permissionDTO.getStatus());
		permission.setOrderIndex(permissionDTO.getOrderIndex());
		permission.setJianpin(permissionDTO.getJianpin());
		permission.setQuanpin(permissionDTO.getQuanpin());
		permission.setName(permissionDTO.getName());
		permission.setIsDelete(permissionDTO.getIsDelete());
		permission.setId(permissionDTO.getId());
		permission.setPermission(permissionDTO.getPermission());
		permission.setDescriprion(permissionDTO.getDescriprion());
		if (isAdd) {
			permission.setCreateName(permissionDTO.getCreateName());
			permission.setCreateTime(permission.getCreateTime());
		} else {
			permission.setModifyName(permissionDTO.getModifyName());
			permission.setModifyTime(permissionDTO.getModifyTime());
		}
		return permission;
	}

	@Override
	public AjaxObj add(PermissionDTO permissionDTO) throws Exception {
		if (permissionDTO == null) {
			return new AjaxObj(0, "参数错误");
		}
		Permission permission = new Permission();
		permissionDao.add(setPermission(permission, permissionDTO, true));
		return new AjaxObj(1, "增加成功");
	}

	@Override
	public AjaxObj deletes(String ids) throws Exception {
		if (StringUtils.isEmpty(ids)) {
			return new AjaxObj(0, "参数错误");
		}
		permissionDao.deletes(ids);
		return new AjaxObj(1, "删除成功");
	}

	@Override
	public AjaxObj update(PermissionDTO permissionDTO) throws Exception {
		if (permissionDTO == null) {
			return new AjaxObj(0, "参数错误");
		}
		Permission permission = permissionDao.load(permissionDTO.getId());
		if (permission == null) {
			return new AjaxObj(0, "数据库异常!");
		}
		permissionDao.update(setPermission(permission, permissionDTO, false));
		return new AjaxObj(1, "更新成功");
	}

}
